<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title>layui</title>
		<meta name="renderer" content="webkit">
		<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
		<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
		<link rel="stylesheet" href="../../lib/layui-v2.5.5/css/layui.css" media="all">
		<link rel="stylesheet" href="../../css/public.css" media="all">


	</head>
	<body>
		<div class="layuimini-container">
			<div class="layuimini-main">

				<fieldset class="table-search-fieldset">
					<legend>添加进程</legend>
					<div style="margin: 10px 10px 10px 10px">
						<form class="layui-form layui-form-pane" action="">
							<div class="layui-form-item">
								<div class="layui-inline">
									<label class="layui-form-label">选择主机</label>
									<div class="layui-input-inline">
										<select id="roomSelect" name="ip" lay-verify="required" lay-search="">
											<option value="">直接选择或搜索选择</option>
										</select>
									</div>
								</div>

								<div class="layui-inline">
									<label class="layui-form-label">进程号(PID)</label>
									<div class="layui-input-inline">
										<input type="text" name="pid" autocomplete="off" lay-verify="number" class="layui-input">
									</div>
								</div>

								<div class="layui-inline">
									<button type="submit" class="layui-btn layui-btn-primary" lay-submit lay-filter="saveBtn">
									<i class="layui-icon layui-icon-addition"></i>   
										添加</button>
								</div>
							</div>
						</form>
					</div>
				</fieldset>
				<script type="text/html" id="toolbarDemo">
					<div class="layui-btn-container">
               
                <button class="layui-btn layui-btn-sm layui-btn-danger data-delete-btn" lay-event="delete"> 删除 </button>
            </div>
        </script>

				<table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter"></table>

				<script type="text/html" id="currentTableBar">

					<a class="layui-btn layui-btn-xs layui-btn-danger data-count-delete" lay-event="delete">删除</a>
        </script>


			</div>
		</div>



		<script src="../../lib/layui-v2.5.5/layui.js" charset="utf-8"></script>
		<script src="../../js/lay-config.js" charset="utf-8"></script>

		<script>
			layui.use(['form', 'table', 'http'], function() {
				var $ = layui.jquery,
					form = layui.form,
					table = layui.table,
					http = layui.http;

				$.ajax({
					type: "post",
					async: false,
					url: http.api + 'pid/findIpAll',
					dataType: "json",
					success: function(response) {
						if (response.code == 0) {
							var ip = response.data;
							for (var i = 0; i < ip.length; i++) {
								var id = i + 1;
								$("#roomSelect").append("<option value=" + ip[i] + ">" + ip[i] + "</option>");
								form.render(); //没有写这个，操作后没有效果
							}

						} else {
							window.history.go(-1);
						}

					},
					error: function(e) {

						alert("请求异常。");
					}
				});


				table.render({
					elem: '#currentTableId',
					url: http.api + 'pid/process',
					toolbar: '#toolbarDemo',
					defaultToolbar: ['filter', 'exports', 'print', {
						title: '提示',
						layEvent: 'LAYTABLE_TIPS',
						icon: 'layui-icon-tips'
					}],
					cols: [
						[{
								type: "checkbox",
								width: 50
							},
							{
								field: 'id',
								width: 0,
								title: 'id',
								hide: 'true'
							},
							{
								field: 'ip',
								//width: 150,
								title: 'ip',
								align: "center"
							},
							{
								field: 'pid',
								//width: 150,
								title: '进程号',
								align: "center"
							},
							{
								field: 'name',
							
								title: '程序名称',
								sort: true,
								align: "center"
							},
							{
								field: 'cpu',

								title: 'cpu',
								sort: true,
								align: "center"
							},
							{
								field: 'mem',

								title: 'mem',
								sort: true,
								align: "center"
							},
							{
								field: 'vsz',

								title: 'vsz',
								sort: true,
								align: "center"
							},
							{
								field: 'rss',

								title: 'rss',
								sort: true,
								align: "center"
							},
{
								field: 'ifsocket',
								title: '进程状态',
								sort: true,
								align: "center",
								templet: function(d) {
									console.log(d)
									if (d.ifsocket == 0) {
										return '<span class="layui-btn layui-btn-xs layui-btn-radius layui-btn-normal">进程有效</span>'
									} else if (d.ifsocket == 1) {
										return '<span class="layui-btn layui-btn-xs layui-btn-radius layui-btn-danger">进程失效</span>'
									} else {
										return '<span class="layui-btn layui-btn-xs layui-btn-radius layui-btn-disabled">待定中</span>'
									}
								}
							},
							{
								title: '操作',
								toolbar: '#currentTableBar',
								align: "center"
							}
						]
					],
					limits: [10, 15, 20, 25, 50, 100],
					limit: 15,
					page: true,
					skin: 'line',
				});

				/**
				 * toolbar监听事件
				 */
				table.on('toolbar(currentTableFilter)', function(obj) {
					if (obj.event === 'delete') { // 监听删除操作
						var checkStatus = table.checkStatus('currentTableId'),
							data = checkStatus.data;
					
						layer.confirm('真的删除么?', function(index) {
							for (j = 0, len = data.length; j < len; j++) {
								var id = data[j].id;
								var ip = data[j].ip;
								var pid = data[j].pid;
								var List = {
									"id": id,
									"ip": ip,
									"pid": pid
								};
							
								$.ajax({
									type: "post",
									async: false,
									url: http.api + 'pid/delete',
									data: List,
									dataType: "json",
									success: function(response) {
										if (response.code == 0) {

										} else {
											window.history.go(-1);
										}

									},
									error: function(e) {

										alert("请求异常。");
									}
								});

							}
							layer.close(index);
							location.reload();
						});

					}
				});
				//监听表格复选框选择
				table.on('checkbox(currentTableFilter)', function(obj) {
			
				});

				table.on('tool(currentTableFilter)', function(obj) {
					var data = obj.data;
		
					if (obj.event === 'delete') {
						layer.confirm('真的删除吗?', function(index) {
							var List = {
								"id": data.id,
								"ip": data.ip,
								"pid": data.pid
							};
							
							$.ajax({
								type: "post",
								async: false,
								url: http.api + 'pid/delete',
								data: List,
								dataType: "json",
								success: function(response) {
									if (response.code == 0) {
										obj.del();
										layer.close(index);

									} else {
										window.history.go(-1);
									}

								},
								error: function(e) {
								
									alert("请求异常。");
								}
							});


						});
					}
				});
				
				//监听提交
				form.on('submit(saveBtn)', function(data) {
						
					$.ajax({
						type: "post",
						async: false,
					    url: http.api + 'pid/add',
						data: data.field,
						dataType: "json",
						success: function(response) {
							if (response.code == 0) {
								var index = parent.layer.getFrameIndex(window.name);
								parent.layer.close(index);
							//修改成功后刷新父界面
							window.parent.location.reload();
							} else {
								window.history.go(-1);
							}
				
						},
						error: function(e) {
							 console.log(data)
							alert("请求异常。");
						}
					});
				
				
				});
				
			});
		</script>

	</body>
</html>
